<?php

class Core_Model_Table_Row_Mail_Template extends Model_Table_Row
{

    /**
     * @var string
     */
    protected $_kind = 'core@mail.template';
    
    /**
     * @var plugin
     */
    protected $_plugin = null;
    
    /**
     * @return int
     */
    public function getId()
    {
        return $this -> template_id;
    }
    
    /**
     * @return string
     */
    public function getModificationDate(){
        return $this->modification_date;
    }
    
    /**
     * @return string
     */
    public function getCreationDate(){
        return $this->creation_date;
    }
    
    /**
     * @return plugin
     */
    public function getPlugin()
    {

        if (null == $this -> _plugin)
        {
            /**
             * check plugin exsits
             */
            if ($this -> plugin)
            {

            }

            /**
             * return default plugin as configured
             */
            $this -> _plugin = new Core_Plugin_Mail_Simple($this);
        }
        return $this -> _plugin;
    }

    /**
     * @param array $params
     * @return array
     */
    public function parse($data)
    {
        return $this -> getPlugin() -> parse($data);
    }

    /**
     * precompile string
     * @param string $string
     * @return string
     */
    protected function _precompile($string)
    {
        $result = preg_match_all("#{{[^\}]+}}#", $string, $match);

        if ($result)
        {
            $match = $match[0];

            foreach ($match as $search)
            {
                if (is_string($search))
                {
                    $replace = sprintf('<?php echo $%s; ?>', trim($search, '{} '));
                    $string = str_replace($search, $replace, $string);
                }
            }
        }

        return $string;
    }
    
    /**
     * @param string $type avaiable value is mail_subject, mail_body
     * @return void
     */
    public function precompile($type)
    {
        $file = $this->getScriptName($type);
        
        $string =  $this->_precompile($this->{$type});
        
        $fp =  fopen($file, 'w');
        
        if(!$fp){
            throw new Exception(sprintf("directory %s or file %s is not writeable!", dirname($file), $file));
        }
        
        fwrite($fp, $string);
        fclose($fp);
    }

    /**
     * compile email template to data
     * @param array $data
     * @return string
     */
    public function compileSubject($data)
    {
       return $this->compile($data, 'mail_subject');
    }

    /**
     * compile email template to data
     * @param array $data
     * @return string
     */
    public function compileBody($data)
    {
        return $this->compile($data,'mail_body');
    }
    
    /**
     * @param array $data
     * @param string $type
     * @return string
     */
    public function compile($data, $type)
    {
        $file = $this -> getScriptName($type);
        
        if (!file_exists($file))
        {
            $this -> precompile($type);
        }
        $data['parser'] = Agis::get('Parser');

        return View::render($data, $file, 0, null);
    }
    
    /**
     * get script file name
     * @param string $type
     * @return string
     */
    public function getScriptName($type)
    {
        return TEMP_PATH . '/mail/' . sha1($this -> getId() . '-' . $type .'-' . $this->getModificationDate(), false) . '.tpl';
    }

}
